import numpy as np
import mmap
import time
from modbus_tk import defines as md
import multiprocessing


class mythread1(multiprocessing.Process):
    def __init__(self, calibration_1, lineEdit_14, i,master):
        self.startData1 = -1
        self.startData2 = -1
        self.startData3 = -1
        self.startData4 = -1
        self.i = 0
        self.x_data1 = []
        self.y_data1 = []
        self.x_data2 = []
        self.y_data2 = []
        self.x_data3 = []
        self.y_data3 = []
        self.x_data4 = []
        self.y_data4 = []
        self.traverse_data(calibration_1, lineEdit_14, i,master)

    # 数据减去第一位
    def dataComputeFirst(self, value, index):
        if index == 1:
            if len(self.x_data1) == 0 or self.startData1 == -1:
                self.value1 = value
            else:
                value = value - self.value1
            return value;
        elif index == 2:
            if len(self.x_data2) == 0 or self.startData2 == -1:
                self.value2 = value
            else:
                value = value - self.value2
            return value;
        elif index == 3:
            if len(self.x_data3) == 0 or self.startData3 == -1:
                self.value3 = value
            else:
                value = value - self.value3
            return value
        elif index == 4:
            if len(self.x_data4) == 0 or self.startData4 == -1:
                self.value4 = value
            else:
                value = value - self.value4
            return value
        else:
            return 0

    # 曲线计算值存储
    def calculateData(self, y_data, x_data, index):
        if index == 1:
            self.x_data1 = np.append(self.x_data1, x_data)
            self.y_data1 = np.append(self.y_data1, y_data)
        elif index == 2:
            self.x_data2 = np.append(self.x_data2, x_data)
            self.y_data2 = np.append(self.y_data2, y_data)
        elif index == 3:
            self.x_data3 = np.append(self.x_data3, x_data)
            self.y_data3 = np.append(self.y_data3, y_data)
        elif index == 4:
            self.x_data4 = np.append(self.x_data4, x_data)
            self.y_data4 = np.append(self.y_data4, y_data)

    # 图形数据展示
    def graphicData(self, data, index):
        return self.dataComputeFirst(data, index)

    def traverse_data(self, calibration_1, lineEdit_14, i,master):
        mmap_file1 = mmap.mmap(0, 1024, access=mmap.ACCESS_WRITE, tagname='share_mmap12')
        self.i = i
        while 1:
            data = []
            x_data = self.i
            # red = []
            # red.append(random.randint(100, 100000))
            # red.append(random.randint(100, 1000))
            # red.append(random.randint(100, 1000))
            # red.append(random.randint(100, 1000))
            red = master.execute(slave=1, function_code=md.READ_INPUT_REGISTERS, starting_address=0,
                                 quantity_of_x=8, output_value=100)
            if len(red)>4:
                # red[0] = red[0]*0.3
                data_0 = calibration_1[0] * int(red[0]) * int(red[0]) + calibration_1[1] * int(
                    red[0]) + \
                         calibration_1[2]
                data_1 = calibration_1[3] * int(red[1]) * int(red[1]) + calibration_1[4] * int(
                    red[1]) + \
                         calibration_1[5]
                data_2 = calibration_1[6] * int(red[2]) * int(red[2]) + calibration_1[7] * int(
                    red[2]) + \
                         calibration_1[8]
                data_3 = calibration_1[9] * int(red[3]) * int(red[3]) + calibration_1[10] * int(
                    red[3]) + \
                         calibration_1[11]
                data_0 = self.graphicData(data_0, 1)
                data_1 = self.graphicData(data_1, 2)
                data_2 = self.graphicData(data_2, 3)
                data_3 = self.graphicData(data_3, 4)

                self.calculateData(data_0, x_data, 1)
                self.calculateData(data_1, x_data, 2)
                self.calculateData(data_2, x_data, 3)
                self.calculateData(data_3, x_data, 4)
                mmap_file1.seek(0)

                data.append(str(self.i).zfill(20))
                data.append(str(x_data).zfill(20))
                data.append(str(data_0).zfill(20))
                data.append(str(data_1).zfill(20))
                data.append(str(data_2).zfill(20))
                data.append(str(data_3).zfill(20))
                data = ",".join(str(i) for i in data)
                mmap_file1.write(data.encode(encoding="utf-8"))
                self.i = self.i + 1
            time.sleep(lineEdit_14)
